const express = require('express');
const router = express.Router();
const { PrismaClient } = require('@prisma/client');
const upload = require('../multer/multer');
const { parseToken } = require('../utils/token');
const prisma = new PrismaClient()
const baseURL = process.env.BASE_URL


/* GET 历史记录 listing. */
// 获取 订阅的用户列表
router.get('/userList', upload.none(), async (req, res) => {
    const token = req.headers.authorization.split(' ')[1]
    const tokenData = await parseToken(token)
    if (!tokenData) return res.status(401).send({ msg: '登录过期' })
    const { id, user } = tokenData
    const { userId } = req.query
    if (id !== userId) return res.status(403).send({msg: '错误的用户信息'})

    const followigUserList = await prisma.followUser.findMany({
        where: {
            followedId: id
        },
        select: {
            followig: {
                select: {
                    id: true,
                    name: true,
                    img: true
                }
            }
        }
    })
    if(followigUserList=== null) return res.status(401).send({msg: '查询错误'})
    const result = followigUserList.map(user => {
        let filePath = ''
        if (user.followig.img) {
            filePath = `${baseURL}/${user.followig.img.replace(/\\/g, '/')}`
        }
        return {
            id: user.followig.id,
            name: user.followig.name,
            img: filePath
        }
    })
    res.status(200).send(result);
});

// 获取 订阅的专栏列表
router.get('/columnList', upload.none(), async (req, res) => {
    const token = req.headers.authorization.split(' ')[1]
    const tokenData = await parseToken(token)
    if (!tokenData) return res.status(401).send({ msg: '登录过期' })
    const { id, user } = tokenData
    const { userId } = req.query
    if (id !== userId) return res.status(403).send({msg: '错误的用户信息'})

    const followigColumnList = await prisma.followColumn.findMany({
        where: {
            userId: id
        },
        select: {
            followColumn: {
                select: {
                    id:true,
                    name: true,
                    mainPath: true,
                    article: true,
                    follow: true,
                    createUser: {
                        select: {
                            id: true,
                            name: true
                        }
                    },
                }
            }
        }
    })
    if(followigColumnList=== null) return res.status(401).send({msg: '查询错误'})
    const result = followigColumnList.map(column => {
        const { followColumn: { id, name, mainPath, article, follow, createUser }} = column
        let filePath = ''
        if (mainPath) {
            filePath = `${baseURL}/${mainPath.replace(/\\/g, '/')}`
        }
        return {
            id: id,
            name: name,
            img: filePath,
            total: article.length,
            followed: follow.length,
            by: createUser.name
        }
    })
    res.status(200).send(result);
});



  


module.exports = router;
